package business

import (
	"github.com/wonderivan/logger"
	"ruoyi-go/app/admin/model/tools"
	"ruoyi-go/pkg/mysql"
	"ruoyi-go/utils"
	"ruoyi-go/utils/R"
	"strings"
)

// Model c5_park_area_info  园区信息表
type C5ParkAreaInfo struct {
	Id              int64  `json:"id" gorm:"primaryKey;column:id" form:"id"`                        //
	ParkAreaCode    string `json:"parkAreaCode" gorm:"park_area_code" form:"parkAreaCode"`          //园区编码
	ParkAreaName    string `json:"parkAreaName" gorm:"park_area_name" form:"parkAreaName"`          //园区名称
	CompanyName     string `json:"companyName" gorm:"company_name" form:"companyName"`              //公司名称
	ContactName     string `json:"contactName" gorm:"contact_name" form:"contactName"`              //联系人
	ContactPhone    string `json:"contactPhone" gorm:"contact_phone" form:"contactPhone"`           //联系电话
	ParkAreaBackup1 string `json:"parkAreaBackup1" gorm:"park_area_backup1" form:"parkAreaBackup1"` //备用字段1
	ParkAreaBackup2 string `json:"parkAreaBackup2" gorm:"park_area_backup2" form:"parkAreaBackup2"` //备用字段2
	ParkAreaBackup3 string `json:"parkAreaBackup3" gorm:"park_area_backup3" form:"parkAreaBackup3"` //备用字段3
	ParkAreaBackup4 string `json:"parkAreaBackup4" gorm:"park_area_backup4" form:"parkAreaBackup4"` //备用字段4
	ParkAreaBackup5 string `json:"parkAreaBackup5" gorm:"park_area_backup5" form:"parkAreaBackup5"` //备用字段5
	CreateId        int64  `json:"createId" gorm:"create_id" form:"createId"`                       //创建者ID
	CreateName      string `json:"createName" gorm:"create_name" form:"createName"`                 //创建者姓名
	CreateTime      string `json:"createTime" gorm:"create_time" form:"createTime"`                 //创建时间
	UpdateId        int64  `json:"updateId" gorm:"update_id" form:"updateId"`                       //更新者ID
	UpdateName      string `json:"updateName" gorm:"update_name" form:"updateName"`                 //更新者姓名
	UpdateTime      string `json:"updateTime" gorm:"update_time" form:"updateTime"`                 //更新时间
	AccountId       string `json:"accountId" gorm:"account_id" form:"accountId"`                    //账户ID
}

func (C5ParkAreaInfo) TableName() string {
	return "c5_park_area_info"
}

// 创建
func CreateC5ParkAreaInfoService(data C5ParkAreaInfo) R.Result {
	err := mysql.MysqlDb().Create(data).Error
	if err != nil {
		logger.Error(err)
		return R.ReturnFailMsg("保存失败")
	}
	return R.ReturnSuccess(nil)
}

// 根据ID删除
func DeleteC5ParkAreaInfoService(id string) R.Result {
	err := mysql.MysqlDb().Delete(&C5ParkAreaInfo{}, "id = ?", id).Error
	if err != nil {
		return R.ReturnFailMsg("删除失败")
	}

	return R.ReturnSuccess(nil)
}

// 根据ID批量删除
func DeleteC5ParkAreaInfoByIdsService(ids string) R.Result {
	if len(ids) == 0 {
		return R.ReturnFailMsg("参数获取失败")
	}

	err := mysql.MysqlDb().Delete(&C5ParkAreaInfo{}, "id in (?)", ids).Error
	if err != nil {
		return R.ReturnFailMsg("批量删除失败")
	}

	return R.ReturnSuccess(nil)
}

// 根据id 更新 ，排除零值
func UpdateC5ParkAreaInfoService(data C5ParkAreaInfo) R.Result {
	err := mysql.MysqlDb().Updates(&data).Error
	if err != nil {
		return R.ReturnFailMsg("更新失败")
	}
	return R.ReturnSuccess(nil)
}

// 根据id获取model
func GetC5ParkAreaInfoService(id int64) R.Result {

	if id == 0 {
		return R.ReturnFailMsg("参数获取失败")
	}
	var data C5ParkAreaInfo
	err := mysql.MysqlDb().Where("id = ?", id).First(&data).Error
	if err != nil {
		return R.ReturnFailMsg("获取数据失败")
	} else {
		return R.ReturnSuccess(data)
	}

}

// 获取所有的model
func GetListC5ParkAreaInfoService() R.Result {
	var list []C5ParkAreaInfo
	err := mysql.MysqlDb().Find(&list).Error
	if err != nil {
		return R.ReturnFailMsg("获取数据失败")
	} else {
		return R.ReturnSuccess(list)
	}
}

// 按条件分页查询 limit offset ,参数用指针&, 数据会自动填充到req对象
func GetPageLimitC5ParkAreaInfoService(params tools.SearchTableDataParam) tools.TableDataInfo {

	var pageNum = params.PageNum
	var pageSize = params.PageSize
	var beginTime = params.Params.BeginTime
	var endTime = params.Params.EndTime

	var total int64
	db := mysql.MysqlDb().Model(C5ParkAreaInfo{})
	otherParams := params.Other.(C5ParkAreaInfo)
	// 可以自定义搜索方式

	// 构建 WHERE 子句
	whereClauses := []string{}
	values := []interface{}{}
	if otherParams.ParkAreaCode != "" {
		whereClauses = append(whereClauses, "park_area_code = ?")
		values = append(values, otherParams.ParkAreaCode)
	}
	if otherParams.ParkAreaName != "" {
		whereClauses = append(whereClauses, "park_area_name LIKE ?")
		values = append(values, "%"+otherParams.ParkAreaName+"%")
	}
	if otherParams.CompanyName != "" {
		whereClauses = append(whereClauses, "company_name LIKE ?")
		values = append(values, "%"+otherParams.CompanyName+"%")
	}
	if otherParams.ContactName != "" {
		whereClauses = append(whereClauses, "contact_name LIKE ?")
		values = append(values, "%"+otherParams.ContactName+"%")
	}
	if otherParams.ContactPhone != "" {
		whereClauses = append(whereClauses, "contact_phone = ?")
		values = append(values, otherParams.ContactPhone)
	}
	if otherParams.ParkAreaBackup1 != "" {
		whereClauses = append(whereClauses, "park_area_backup1 = ?")
		values = append(values, otherParams.ParkAreaBackup1)
	}
	if otherParams.ParkAreaBackup2 != "" {
		whereClauses = append(whereClauses, "park_area_backup2 = ?")
		values = append(values, otherParams.ParkAreaBackup2)
	}
	if otherParams.ParkAreaBackup3 != "" {
		whereClauses = append(whereClauses, "park_area_backup3 = ?")
		values = append(values, otherParams.ParkAreaBackup3)
	}
	if otherParams.ParkAreaBackup4 != "" {
		whereClauses = append(whereClauses, "park_area_backup4 = ?")
		values = append(values, otherParams.ParkAreaBackup4)
	}
	if otherParams.ParkAreaBackup5 != "" {
		whereClauses = append(whereClauses, "park_area_backup5 = ?")
		values = append(values, otherParams.ParkAreaBackup5)
	}
	if otherParams.CreateId != 0 {
		whereClauses = append(whereClauses, "create_id = ?")
		values = append(values, otherParams.CreateId)
	}
	if otherParams.CreateName != "" {
		whereClauses = append(whereClauses, "create_name LIKE ?")
		values = append(values, "%"+otherParams.CreateName+"%")
	}
	if otherParams.CreateTime != "" {
	}
	if otherParams.UpdateId != 0 {
		whereClauses = append(whereClauses, "update_id = ?")
		values = append(values, otherParams.UpdateId)
	}
	if otherParams.UpdateName != "" {
		whereClauses = append(whereClauses, "update_name LIKE ?")
		values = append(values, "%"+otherParams.UpdateName+"%")
	}
	if otherParams.UpdateTime != "" {
		whereClauses = append(whereClauses, "update_time = ?")
		values = append(values, otherParams.UpdateTime)
	}
	if otherParams.AccountId != "" {
		whereClauses = append(whereClauses, "account_id = ?")
		values = append(values, otherParams.AccountId)
	}

	if beginTime != "" {
		whereClauses = append(whereClauses, "create_time >= ?")
		beginTimeStr := utils.GetBeginTime(beginTime)
		values = append(values, beginTimeStr)
	}

	if endTime != "" {
		whereClauses = append(whereClauses, "create_time <= ?")
		endTimeStr := utils.GetBeginTime(endTime)
		values = append(values, endTimeStr)
	}

	if len(whereClauses) > 0 {
		db = db.Where(strings.Join(whereClauses, " AND "), values...)
	}

	var rows []C5ParkAreaInfo

	if err := db.Count(&total).Error; err != nil {
		return tools.Fail()
	}
	offset := (pageNum - 1) * pageSize
	db.Order("id DESC").Offset(offset).Limit(pageSize).Find(&rows)
	if rows == nil {
		return tools.Fail()
	} else {
		return tools.Success(rows, total)
	}
}
